package com.ruoyi.ctcx.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ctcx.domain.TbInsuranceContractRiskPlan;
import com.ruoyi.ctcx.domain.TbInsuranceRiskContract;
import com.ruoyi.ctcx.service.ITbInsuranceContractRiskPlanService;
import com.ruoyi.ctcx.service.ITbInsuranceRiskContractService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ctcx.domain.TbInsuranceContractRiskReal;
import com.ruoyi.ctcx.service.ITbInsuranceContractRiskRealService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 保险合同风控费用支付Controller
 * 
 * @author ruoyi
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/riskContract/real")
public class TbInsuranceContractRiskRealController extends BaseController
{
    @Autowired
    private ITbInsuranceContractRiskRealService tbInsuranceContractRiskRealService;

    @Autowired
    private ITbInsuranceContractRiskPlanService tbInsuranceContractRiskPlanService;

    @Autowired
    private ITbInsuranceRiskContractService tbInsuranceRiskContractService;

    /**
     * 查询保险合同风控费用支付列表
     */
    @PreAuthorize("@ss.hasPermi('riskContract:real:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbInsuranceContractRiskReal tbInsuranceContractRiskReal)
    {
        startPage();
        List<TbInsuranceContractRiskReal> list = tbInsuranceContractRiskRealService.selectTbInsuranceContractRiskRealList(tbInsuranceContractRiskReal);
        return getDataTable(list);
    }

    /**
     * 导出保险合同风控费用支付列表
     */
    @PreAuthorize("@ss.hasPermi('riskContract:real:export')")
    @Log(title = "保险合同风控费用支付", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbInsuranceContractRiskReal tbInsuranceContractRiskReal)
    {
        List<TbInsuranceContractRiskReal> list = tbInsuranceContractRiskRealService.selectTbInsuranceContractRiskRealList(tbInsuranceContractRiskReal);
        ExcelUtil<TbInsuranceContractRiskReal> util = new ExcelUtil<TbInsuranceContractRiskReal>(TbInsuranceContractRiskReal.class);
        util.exportExcel(response, list, "保险合同风控费用支付数据");
    }

    /**
     * 获取保险合同风控费用支付详细信息
     */
    @PreAuthorize("@ss.hasPermi('riskContract:real:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbInsuranceContractRiskRealService.selectTbInsuranceContractRiskRealById(id));
    }

    /**
     * 新增保险合同风控费用支付
     */
    @PreAuthorize("@ss.hasPermi('riskContract:real:add')")
    @Log(title = "保险合同风控费用支付", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbInsuranceContractRiskReal tbInsuranceContractRiskReal)
    {

        //保存实际费用
        tbInsuranceContractRiskReal.setCreateBy(getUsername());

        Long planId = tbInsuranceContractRiskReal.getPlanId();
        if (null != planId)
        {
            TbInsuranceContractRiskPlan plan =  tbInsuranceContractRiskPlanService.selectTbInsuranceContractRiskPlanById(planId);
            if (null != plan)
            {
                tbInsuranceContractRiskReal.setRealName(plan.getPlanName());
            }
        }


        tbInsuranceContractRiskRealService.insertTbInsuranceContractRiskReal(tbInsuranceContractRiskReal);

        //更新保险合同风控费用
        BigDecimal realAmount = tbInsuranceContractRiskRealService.getAllRealAmountByRiskId(tbInsuranceContractRiskReal.getRiskId());
        TbInsuranceRiskContract tbInsuranceRiskContract =  tbInsuranceRiskContractService.selectTbInsuranceRiskContractById(tbInsuranceContractRiskReal.getRiskId());
        if (null != tbInsuranceRiskContract)
        {
            tbInsuranceRiskContract.setRiskAmountReal(realAmount);
            tbInsuranceRiskContractService.updateTbInsuranceRiskContract(tbInsuranceRiskContract);
        }
        return toAjax(1);
    }

    /**
     * 修改保险合同风控费用支付
     */
    @PreAuthorize("@ss.hasPermi('riskContract:real:edit')")
    @Log(title = "保险合同风控费用支付", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbInsuranceContractRiskReal tbInsuranceContractRiskReal)
    {
        tbInsuranceContractRiskReal.setUpdateBy(getUsername());

        Long planId = tbInsuranceContractRiskReal.getPlanId();
        if (null != planId)
        {
            TbInsuranceContractRiskPlan plan =  tbInsuranceContractRiskPlanService.selectTbInsuranceContractRiskPlanById(planId);
            if (null != plan)
            {
                tbInsuranceContractRiskReal.setRealName(plan.getPlanName());
            }
        }

        tbInsuranceContractRiskRealService.updateTbInsuranceContractRiskReal(tbInsuranceContractRiskReal);

        //更新保险合同风控费用
        BigDecimal realAmount = tbInsuranceContractRiskRealService.getAllRealAmountByRiskId(tbInsuranceContractRiskReal.getRiskId());
        TbInsuranceRiskContract tbInsuranceRiskContract =  tbInsuranceRiskContractService.selectTbInsuranceRiskContractById(tbInsuranceContractRiskReal.getRiskId());
        if (null != tbInsuranceRiskContract)
        {
            tbInsuranceRiskContract.setRiskAmountReal(realAmount);
            tbInsuranceRiskContractService.updateTbInsuranceRiskContract(tbInsuranceRiskContract);
        }

        return toAjax(1);
    }

    /**
     * 删除保险合同风控费用支付
     */
    @PreAuthorize("@ss.hasPermi('riskContract:real:remove')")
    @Log(title = "保险合同风控费用支付", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        for (Long id : ids)
        {
            TbInsuranceContractRiskReal tbInsuranceContractRiskReal = tbInsuranceContractRiskRealService.selectTbInsuranceContractRiskRealById(id);
            tbInsuranceContractRiskRealService.deleteTbInsuranceContractRiskRealById(id);

            //更新保险合同风控费用
            BigDecimal realAmount = tbInsuranceContractRiskRealService.getAllRealAmountByRiskId(tbInsuranceContractRiskReal.getRiskId());
            TbInsuranceRiskContract tbInsuranceRiskContract =  tbInsuranceRiskContractService.selectTbInsuranceRiskContractById(tbInsuranceContractRiskReal.getRiskId());
            if (null != tbInsuranceRiskContract)
            {
                tbInsuranceRiskContract.setRiskAmountReal(realAmount);
                tbInsuranceRiskContractService.updateTbInsuranceRiskContract(tbInsuranceRiskContract);
            }
        }



        return toAjax(1);
    }
}
